<?php
defined( '_JEXEC' ) or die( 'Restricted access' );

/**
* Crea un SEF a partir del query de la URL.
* @param Array $query
*/
function linioBuildRoute( &$query ){
    $tarea = null;
	$segments = array();

	// codigo para evitar que en la url aparezca "/component/linio"
	$menu = JFactory::getApplication()->getMenu();
	$items = $menu->getItems('component', 'com_linio');
	if (!isset($query['Itemid']) && isset($items) && count($items) > 0)  {
		$query['Itemid'] =  $items[0]->id;
	}     	 	
	// fin //	
	if(isset($query['country'])){
		$segments[] = $query['country'];
		unset( $query['country'] );
	}		
	if(isset($query['task'])){
	    $tarea = $query['task'];
		$segments[] = $tarea;
		unset( $query['task'] );
	}
	if(isset($query['view'])){
		$segments[] = $query['view'];
		unset( $query['view'] );
	}	
	if($tarea == 'category'){
		if(isset($query['category'])){
			$segments[] = $query['category'];
			unset( $query['category'] );
		}	
		if(isset($query['brand'])){
			$segments[] = $query['brand'];
			unset( $query['brand'] );
		}			
	}
	if($tarea == 'search'){
		if(isset($query['search'])){
			$segments[] = $query['search'];
			unset( $query['search'] );
		}
	}
	if($tarea == 'producto'){
		if(isset($query['itemId'])){
			$segments[] = $query['itemId'];
			unset( $query['itemId'] );
		}
		if(isset($query['description'])){
			$segments[] = $query['description'];
			unset( $query['description'] );
		}		
	}
	if($tarea == 'brand'){
		if(isset($query['brand'])){
			$segments[] = $query['brand'];
			unset( $query['brand'] );
		}
	}	
    return $segments;
}
/**
*
* Parsea la URL para recuperar las variables del SEF
* @param Array $segments
*/
function linioParseRoute( $segments ){
    //echo '[linioParseRoute][segments]:'.print_r($segments);	
	$vars = array();
	if($segments){
		$vars['country'] = $segments[0];
		if(isset($segments[1])){
			$vars['task'] = $segments[1];	
		} else {
			$vars['task'] = 'home';
		}		
		if($vars['task'] == 'category'){
			if(sizeof($segments) == 3){
				$vars['category'] = $segments[2];			
			}
			if(sizeof($segments) == 4){
				$vars['category'] = $segments[2];		
				$vars['brand'] = $segments[3];			
			}	
			if(sizeof($segments) == 5){
				$vars['view'] = $segments[2];			
				$vars['category'] = $segments[3];			
				$vars['brand'] = $segments[4];			
			}		
		}		
		if($vars['task'] == 'brand'){
			$vars['brand'] = $segments[2];
		}
		if($vars['task'] == 'search'){
			$vars['search'] = $segments[2];
		}		
		if($vars['task'] == 'producto'){
			$vars['itemId'] = $segments[2];
			$vars['description'] = $segments[3];			
		}	
		if($vars['task'] == 'sitemap'){
			$vars['format'] = 'sitemap';			
		}	
	} else {
		$vars['view'] = null;
		$vars['country'] = 'MX';
		$vars['task'] = null;
	}
	//echo '[linioParseRoute][vars]:'.print_r($vars);	
    return $vars;
}
?>